package com.turnengine.client.global.game.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.bean.GameInstanceSerializer;
import com.turnengine.client.global.game.bean.IGameInstance;
import java.io.IOException;

/**
 * The Get Game Instance By Name Data Serializer.
 */
public class GetGameInstanceByNameDataSerializer extends AbstractCommandDataSerializer<IGameInstance> implements IGetGameInstanceByNameDataSerializer {

	/**
	 * Creates a new Get Game Instance By Name Data Serializer.
	 */
	public GetGameInstanceByNameDataSerializer() {
		super(GetGameInstanceByName.COMMAND_ID);
	}

	@Override
	public IGameInstance readReturnValue(IDataReader reader) throws IOException {
		IGameInstance value = new GameInstanceSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGameInstance value) throws IOException {
		new GameInstanceSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGameInstance> readRequest(IDataReader reader) throws IOException {

		// Read fields
		String instanceName = reader.readString(true);
		int versionId = reader.readInt();

		// Create object
		GetGameInstanceByName object = new GetGameInstanceByName();
		object.setInstanceName(instanceName);
		object.setVersionId(versionId);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGameInstance> argument) throws IOException {
		GetGameInstanceByName object = (GetGameInstanceByName) argument;

		// Get fields
		String instanceName = object.getInstanceName();
		int versionId = object.getVersionId();

		// Write fields
		writer.writeString(instanceName, true);
		writer.writeInt(versionId);
	}

	@Override
	public IExecutableCommandResponse<IGameInstance> newResponse() {
		return new GetGameInstanceByNameResponse();
	}
}
